package com.mvp.shq.washer.ui.feendBack;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/9/4.
 */

public class Presenter extends FeedBackConstract.Presenter {
    @Override
    public void onStart() {

    }


    @Override
    void feedBack(String message, List<File> files) {
        Logger.d("图片数量："+files.size());
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        Map<String, RequestBody> params = new HashMap<>();
        for (int i=0;i<files.size();i++){
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),files.get(i));
            params.put("photos\";filename=\""+i+"+image.png" ,requestBody);
        }
        if (files == null || files.size() <= 0){
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),"");
            params.put("nophoto",requestBody);
        }
        model.feedBack(token,userId,message,params)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.feedBackF(t.getMsg());
                        }else {
                            view.feedBackS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                        view.feedBackF("未知错误");
                    }
                });
    }
}
